package com.doge.walknovel.business.novel.reply

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.doge.walknovel.api.model.CommentInfo
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.toLiveData

class ReplyViewModel constructor(private val repository: ReplyRepository) : ViewModel() {
    val loading: LiveData<LoadStatus> = repository.loading
    val dataList = repository.dataSource().toLiveData(
        cellId = { it.id },
        requestMore = { repository.requestCommentReplies(false) })

    fun requestCommentReplies() = repository.requestCommentReplies(true)

    fun requestCollectReply(data: CommentInfo?) {
        if (data == null) return
        if (data.likeFlag == 1) repository.requestUnLikeReplay(data)
        else repository.requestLikeReply(data)
    }
}


class ReplyViewModelFactory(private val requestId: Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = ReplyRepository(requestId)
        if (modelClass.isAssignableFrom(ReplyViewModel::class.java)) {
            return ReplyViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

